<template>
  <view>
    <uni-search-bar placeholder="请输入内容" @cancel="getcontractInfoClear" @confirm="getcontractInfo"
      v-if="usertype==2||usertype==5" bgColor="white"></uni-search-bar>

    <view class="xuanxiangka" v-if="usertype==2||usertype==5" style="justify-content: space-around;">
      <view :class="cindex==0?'xuanzhong':''" @click="getcontractstatus('全部',0)">全部</view>
      <view :class="cindex==1?'xuanzhong mincontract':'mincontract'" @click="getcontractstatus('编辑中',1)">编辑中</view>
      <view :class="cindex==2?'xuanzhong mincontract':'mincontract'" @click="getcontractstatus('已签约',2)">已签约</view>
      <view :class="cindex==3?'xuanzhong mincontract':'mincontract'" @click="getcontractstatus('待签约',3)">待签约</view>
    </view>


    <view class="null-box" v-if="contractmin.length == 0">
      <view class="null-image">
        <image src="../../../static/images/777.png" mode=""></image>
      </view>
      空空如也
    </view>

    <view class="xuanxiangka" v-if="usertype!=2&&usertype!=5&&contractmin.length != 0">
      <view :class="cindex==0?'xuanzhong':''" @click="getmincontract(0)" v-if="usertype!=4&&usertype!=2&&usertype!=5">
        劳工合同</view>
      <view :class="cindex==1?'xuanzhong mincontract':'mincontract'" v-if="usertype!=2&&usertype!=5"
        @click="getmincontract(1)">我的合同</view>
    </view>

    <view class="contract-list" style="padding-bottom: 80rpx;" v-for="(item,index) in contractmin" :key="index"
      @click="tocontractmodel(item.id,item.contractStatus)">
      <view class="contract-title">
        <view class="contract-img">
          <view class="image">
            <image src="../../../static/images/组 336.png"></image>
          </view>
          <view class="title-text">
            {{item.projectName}}{{agreementOrContract(item.workerIdentityCard,item.workerSex)?'签约合同':'签约协议'}}
          </view>
        </view>
        <view class="contract-tright">
          <view :class="item.contractStatus==='已签约'?'point1':'point2'"></view>
          <view :class="item.contractStatus==='已签约'?'yfontcolor':'nfontcolor'">{{item.contractStatus}}</view>
          <!-- 状态要更改 -->
        </view>
      </view>
      <view class="contract-info">
        <view class="contract-itext">
          <view class="">合同编号：{{item.contractId}}</view>
          <view class="">发行日期:{{item.contractWorkStart.replace('00:00:00', '')}}</view>
        </view>
        <view class="contract-itext" style="margin-left: 10rpx;">
          <view class="">签约人：{{item.workerName}}</view>
          <view class="">签约日期:{{item.contractPersonTwoTime == null ? '未签约' : item.contractPersonTwoTime.replace('00:00:00', '')}}</view>
        </view>
      </view>
    </view>
<view class="sendcontract"  v-if="cindex == 0 && usertype != 4 && usertype != ''">
      <button @click="addContract(1)">发起简约合同</button>
	  <button @click="addContract(0)">发起详细合同</button>

    </view>


  </view>
</template>
<script>
  export default {
    data() {
      return {
        detail: '',
        nickName: '',
        projectId: '',
        projectName: '',
        usertype: 4,
        cindex: 0,
        contractStatus: 0,
        contractWorkEnd: "",
        contractWorkStart: "",
        contractWorkTimeType: 0,
        id: 0,
        isEffective: 1,
        orderBy: "",
        pageNo: 1,
        pageSize: 100000,
        returnFields: "",
        unitName: "",
        workerName: "",
        contract: [],
        contractmin: [],
        contractStatus: []
      }
    },
    onLoad(options) {
      if (options.card != null) {
        console.log('--------onload');
        this.detail = '234'
        this.usertype = 4
        this.detailcontract(options.card)
        return
      }
      this.usertype = uni.getStorageSync('usertype');
      this.nickName = uni.getStorageSync('nickName')
    },
    created() {
      if (this.detail.length == 0) {
        console.log('----------created');
        this.getContract();
      }
    },
    methods: {
		agreementOrContract(idCode,sex){//年龄判断超出显示协议
				  if(sex==0&&this.calculateAge(idCode)>=50){
					  return false
				  }
				  if(sex==1&&this.calculateAge(idCode)>=60){
					  return false
				  }
				  return true
		},
		calculateAge(idNumber) {
				  // 从身份证号码中提取出生日期，假设身份证号码中出生日期的格式为"YYYYMMDD"
				   const birthYear = parseInt(idNumber.substr(6, 4), 10); const birthMonth = parseInt(idNumber.substr(10, 2), 10);
					const birthDay = parseInt(idNumber.substr(12, 2), 10); 
					// 获取当前日期 
					const currentDate = new Date(); 
					const currentYear = currentDate.getFullYear(); 
					const currentMonth = currentDate.getMonth() + 1; 
					// 月份从0开始，所以要加1
					 const currentDay = currentDate.getDate();
					  // 计算年龄 
					  let age = currentYear - birthYear; 
					  // 根据出生月份和日期判断是否已经过生日 
					  if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) { age--; }
					   return age; 
		},
      // 0 表示已签约 1编辑中 2待签约
      tocontractmodel(id, status) { //点击合同列表执行跳转方法
        uni.navigateTo({
          url: './signingcontract?id=' + id + '&status=' + status
        })
      },
      addContract(num) {
        uni.redirectTo({
          url: './contractmodel?id=' + num
        })
      },
      changecolor(num) {
        this.index = num;
      },
      getContract() {
        this.cindex = 0
        uni.showLoading({
          title: '数据加载中'
        })
        uni.$post(this.path + '/contract/queryAll', {}).then(res => {
          if (res.code == 200) {
            this.contract = res.data.records
            this.contract.sort((a, b) => {
              return b.contractStatus - a.contractStatus
            })
            // 0 表示已签约 1编辑中 2待签约
            for (let i = 0; i < this.contract.length; i++) {
              let item = this.contract[i]
              if (item.contractStatus == 0) {
                item.contractStatus = '已签约'
              } else if (item.contractStatus == 1) {
                item.contractStatus = '编辑中'
              } else {
                item.contractStatus = '待签约'
              }
              if (item.contractPersonTwoTime == null) {
                item.contractPersonTwoTime = '未签约'
              }
              item.contractWorkAddress = item.contractWorkAddress + '签约合同'
            }
            this.contractmin = this.contract;
            uni.hideLoading()
          } else {
            uni.hideLoading()
            uni.showToast({
              title: '数据加载失败, 请重试'
            })
          }
        })
        this.cindex = 0
      },

      detailcontract(card) {
        this.cindex = 1
        uni.showLoading({
          title: '数据加载中'
        })
        uni.$post(this.path + '/contract/queryAll', {
          personCard: card
        }).then(res => {
          if (res.code == 200) {
            this.contract = res.data.records
            this.contract.sort((a, b) => {
              return b.contractStatus - a.contractStatus
            })
            // 0 表示已签约 1编辑中 2待签约
            for (let i = 0; i < this.contract.length; i++) {
              let item = this.contract[i]
              if (item.contractStatus == 0) {
                item.contractStatus = '已签约'
              } else if (item.contractStatus == 1) {
                item.contractStatus = '编辑中'
              } else {
                item.contractStatus = '待签约'
              }
              item.contractWorkAddress = item.contractWorkAddress + '签约合同'
              if (item.contractPersonTwoTime == null) {
                item.contractPersonTwoTime = '未签约'
              }

            }
            this.contractmin = this.contract
            uni.hideLoading()
          } else {
            uni.hideLoading()
            uni.showToast({
              title: '数据加载失败, 请重试',
              icon: 'error',
              duration: 1000
            })
            setTimeout(() => {
              uni.navigateBack();
            }, 1000)
          }
        })
      },


      getmincontract(id) {
        //点击我的合同 然后你通过名字来过滤自己名字的合同
        this.cindex = 1
        this.contractmin = []
        for (let i = 0; i < this.contract.length; i++) {
          if (this.nickName == this.contract[i].workerName) {
            this.contractmin.push(this.contract[i])
          }
        }
      },
      getcontractstatus(str, num) {
        this.cindex = num
        // console.log('huhuhuhu');
        this.contractmin = []
        // 如果num 0 是表示展示全部
        if (num == 0) {
          this.contractmin = this.contract
          return
        }
        for (let i = 0; i < this.contract.length; i++) {
          if (this.contract[i].contractStatus == str) {
            this.contractmin.push(this.contract[i])
          }
        }
      },
      getcontractInfo(e) {
        console.log('------搜索触发了');
        this.contractmin = []
        console.log('e', e);
        let mySet = new Set() // 防止一行中出现多个匹配的值 然后重复添加
        console.log(this.contract);
        this.contract.forEach(item => {
          if (item.contractWorkAddress.indexOf(e.value) != -1) {
            mySet.add(item)
          }
          if (item.contractId.indexOf(e.value) != -1) {
            mySet.add(item)
          }
          if (item.workerName.indexOf(e.value) != -1) {}
          if (item.contractWorkStart.indexOf(e.value) != -1) {
            mySet.add(item)
          }
          if (item.contractPersonTwoTime.indexOf(e.value) != -1) {
            mySet.add(item)
          }
        })
        this.contractmin = Array.from(mySet);
        console.log('contractmin', this.contractmin);
      },
      getcontractInfoClear() {

        this.contractmin = this.contract
      }
    }
  }
</script>

<style lang="scss">
  .null-box {
    margin-top: 140rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .point1 {
    width: 20rpx;
    height: 20rpx;
    border-radius: 50%;
    background-color: #2f67ff;
  }

  .point2 {
    width: 20rpx;
    height: 20rpx;
    border-radius: 50%;
    background-color: #ff0000;
  }


  .sendcontract {
    width: 100%;
    height: 35px;
    position: fixed;
    bottom: 10px;


    display: flex;
    justify-content: space-around;
    button {
      width: 40%;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      margin: 0 auto;
      color: white;
      background-color: #2f67ff;
    }
  }
  

  .xuanxiangka {
    display: flex;
    margin: 0 auto;
    margin-top: 10px;
    width: 90%;
    height: 20px;
    color: #b4b4b4;
    font-size: 14px;
    align-items: center;

    .mincontract {
      margin-left: 40px;
    }

    .xuanzhong {
      color: black;
      font-size: 16px;
    }


  }

  .contract-list {
    width: 95%;
    margin: 0 auto;
    margin-top: 10px;
    height: 110px;
    background-color: white;
    padding-top: 10px;
    border-radius: 8px;

    .contract-title {
      font-size: 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 95%;
      margin: 0 auto;

      .contract-img {
        width: calc(100% - 100px);
        height: 30px;
        display: flex;
        align-items: center;

        .image {
          width: 30px;
          height: 30px;

          image {
            width: 100%;
            height: 100%;
          }
        }

        .title-text {
          font-size: 16px;
        }
      }

      .contract-tright {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 60px;

        .yfontcolor {
          color: #2f67ff;
        }

        .nfontcolor {
          color: #ff0000;
        }
      }
    }

    .contract-info {
      margin-top: 10px;
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: center;
      align-items: center;

      .contract-itext {
        width: fit-content;
        min-width: 241rpx;
        height: 50px;
        font-size: 12px;
        color: #b4b4b4;
        display: flex;
        justify-content: space-between;
        flex-direction: column;
      }
    }
  }
</style>
